<template>
    <div>
        <div class="child-content">
            <div class="item-vip-card" v-for="(item, index) in vipList" :key="index">
                <div class="item-vip-bg"></div>
                <div class="item-vip-type">{{ item.rechargeName }}</div>
                <div class="item-vip-content">
                    <div class="item-vip-price-score">
                        <div class="item-vip-price">￥ <span class="item-vip-price-num">{{ item.rechargeMoneyFmt }}</span>
                        </div>
                        <div class="item-get-score">得{{ item.score }} <img class="item-score-icon"
                                src="@/assets/images/score-icon.png" alt=""></div>
                    </div>
                    <div class="item-get-benefit">
                        <div class="item-get-benefit-label">获得权益</div>
                        <div class="item-get-benefit-desc" v-for="(val, idx) in item.rights" :key="idx">
                            {{ idx + 1 }}.{{ val }}</div>
                    </div>
                    <div class="to-buy-btn" @click="toShowDialog(item)">立即购买</div>
                </div>
            </div>

        </div>

        <div class="dialog-mask-box" v-if="dialogVisible">
            <div class="dialog-box">
                <div class="item-vip-bg"> </div>
                <div class="item-vip-type">{{ selectedVip.rechargeName }}</div>
                <div class="item-close-box" @click="toCloseDialog"> <img src="@/assets/images/close-icon.png" alt="">
                </div>
                <div class="item-vip-content">
                    <div class="item-vip-price-score">
                        <div class="item-vip-price">￥ <span
                                class="item-vip-price-num">{{ selectedVip.rechargeMoneyFmt }}</span></div>
                        <div class="item-get-score">得{{ selectedVip.score }} <img class="item-score-icon"
                                src="@/assets/images/score-icon.png" alt=""></div>
                    </div>
                    <div class="show-wx-qr-code">
                        <img v-if="codeUrl" :src="codeUrl" alt="">
                    </div>
                    <div class="show-wx-pay-label">请使用微信扫码付款</div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ElMessageBox, ElMessage } from 'element-plus'
import Qrcode from 'qrcode'
import { getVipList, rechargeVip } from "@/api/vip";
import { getOrderPayResult } from "@/api/index";
import { onMounted } from "vue";

const dialogVisible = ref(false);
const vipList = ref([]);
const selectedVip = ref({});
const codeUrl = ref('');
const downTime = ref(60);
const orderNo = ref('');
const timer = ref(null);

const toShowDialog = (item) => {
    selectedVip.value = item;
    toRechargeVip()
}
const toCloseDialog = () => {
    dialogVisible.value = false;
    selectedVip.value = {};
    codeUrl.value = '';
    orderNo.value = '';
    downTime.value = 60;
    clearTimeout(timer.value);
    timer.value = null;
}

const toGetVipList = async () => {
    let res = await getVipList();
    if (res) {
        vipList.value = res.data;
    }
}

const toRechargeVip = async () => {
    let res = await rechargeVip({
        rechargeId: selectedVip.value.rechargeId,
        payMethod: 'wechat'
    });
    if (res) {
        let { data } = res;
        orderNo.value = data.orderNo;
        Qrcode.toDataURL(data.code_url).then(url => {
            codeUrl.value = url;
            dialogVisible.value = true;
            toCountDown()
        })
    }
}

const toPay = async () => {
    let res = await getOrderPayResult(orderNo.value);
    if (res) {
        let { data } = res
        if (data.payStatus == '1') {
            ElMessage.success('支付成功')
            clearTimeout(timer.value);
            toCloseDialog();
        }
    }
}

const toCountDown = () => {
    if (downTime.value > 0) {
        downTime.value--;
        toPay()
        timer.value = setTimeout(() => {
            toCountDown();
        }, 3000);
    } else {
        toCloseDialog();
    }
}

onMounted(() => {
    toGetVipList();
});
</script>

<style lang="scss" scoped>
.child-content {
    display: flex;
    flex-wrap: wrap;
}

.item-vip-card {
    width: 270px;
    height: 450px;
    border: 1px solid rgba(209, 227, 255, 1);
    border-radius: 8px;
    background: rgba(255, 255, 255, 1);
    position: relative;
    margin-bottom: 22px;

    &:not(:nth-child(3n)) {
        margin-right: 22px;
    }

    .item-get-benefit {
        padding: 16px 0 32px;
        color: rgba(145, 153, 163, 1);
        font-size: 14px;
        font-weight: 500;
        line-height: 24px;
    }

    .to-buy-btn {
        position: absolute;
        bottom: 30px;
        left: 50%;
        transform: translateX(-50%);
        width: 124px;
        height: 40px;
        border-radius: 8px;
        background: rgba(248, 216, 184, 1);
        text-align: center;
        line-height: 40px;
        color: rgba(159, 94, 10, 1);
        font-size: 15px;
        font-weight: 900;
        cursor: pointer;
    }
}

.dialog-mask-box {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.3);
    z-index: 9999;
    display: none;
    display: flex;
    align-items: center;
    justify-content: center;

    .dialog-box {
        width: 400px;
        height: 420px;
        border: 1px solid rgba(209, 227, 255, 1);
        border-radius: 8px;
        background: rgba(255, 255, 255, 1);
        position: relative;
    }

    .item-close-box {
        position: absolute;
        top: 16px;
        right: 16px;
        width: 24px;
        height: 24px;
        cursor: pointer;
        z-index: 10;
    }

    .show-wx-qr-code {
        width: 177px;
        height: 177px;
        background: #000;
        margin: 16px auto 32px;
    }

    .show-wx-pay-label {
        color: rgba(159, 94, 10, 1);
        font-size: 15px;
        font-weight: 900;
        line-height: 24px;
        text-align: center;
    }
}

.item-vip-content {
    position: relative;
    z-index: 1;
    width: 100%;
    height: 100%;
    padding: 70px 24px 0;
}

.item-vip-bg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 200px;
    background: linear-gradient(180.00deg, rgba(208, 226, 255, 1), rgba(255, 255, 255, 0) 100%);
}

.item-vip-type {
    width: 84px;
    height: 32px;
    line-height: 32px;
    padding: 0 0 0 13px;
    color: rgba(255, 255, 255, 1);
    font-size: 18px;
    font-weight: 700;
    position: absolute;
    top: 16px;
    left: -1px;
    z-index: 5;
    background-image: url("@/assets/images/card-type-bg.png");
    background-repeat: no-repeat;
    background-size: cover;
}

.item-vip-price-score {
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding-bottom: 28px;
    border-bottom: 1px solid rgba(215, 230, 255, 1);

    .item-vip-price {
        display: flex;
        align-items: flex-end;
        color: rgba(5, 11, 32, 1);
        font-size: 14px;
        font-weight: 700;
        line-height: 0.1;
        margin-right: 16px;

        .item-vip-price-num {
            font-size: 48px;
            line-height: 30px;
        }
    }

    .item-get-score {
        display: flex;
        align-items: center;
        color: rgba(5, 11, 32, 1);
        font-size: 16px;
        font-weight: 500;
        line-height: 1;
        position: relative;
        top: 5px;

        .item-score-icon {
            width: 16px;
            height: 16px;
            margin-left: 3px;
        }
    }
}
</style>